package com.ruoyi.answerRecord.service.impl;

import java.util.List;

import com.ruoyi.common.utils.CodeGeneratorUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.points.dao.IPointsChangeRecordDao;
import com.ruoyi.question.mapper.QuestionInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.answerRecord.mapper.UserQuizAnswerRecordMapper;
import com.ruoyi.answerRecord.domain.UserQuizAnswerRecord;
import com.ruoyi.answerRecord.service.IUserQuizAnswerRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户答题记录管理Service业务层处理
 * 
 * @author amy
 * @date 2025-06-12
 */
@Service
public class UserQuizAnswerRecordServiceImpl implements IUserQuizAnswerRecordService 
{
    @Autowired
    private UserQuizAnswerRecordMapper userQuizAnswerRecordMapper;

    @Autowired
    private QuestionInfoMapper questionInfoMapper;

    @Autowired
    private IPointsChangeRecordDao pointsChangeRecordDao;

    /**
     * 查询用户答题记录管理
     * 
     * @param id 用户答题记录管理主键
     * @return 用户答题记录管理
     */
    @Override
    public UserQuizAnswerRecord selectUserQuizAnswerRecordById(Long id)
    {
        return userQuizAnswerRecordMapper.selectUserQuizAnswerRecordById(id);
    }

    /**
     * 查询所有用户答题记录
     *
     * @param userQuizAnswerRecord 用户答题记录管理
     * @return 用户答题记录管理集合
     */
    @Override
    public List<UserQuizAnswerRecord> selectAllQuizAnswerRecordList(UserQuizAnswerRecord userQuizAnswerRecord) {
        return userQuizAnswerRecordMapper.selectAllQuizAnswerRecordList(userQuizAnswerRecord);
    }

    /**
     * 查询用户答题记录管理列表
     * 
     * @param userQuizAnswerRecord 用户答题记录管理
     * @return 用户答题记录管理
     */
    @Override
    public List<UserQuizAnswerRecord> selectUserQuizAnswerRecordList(UserQuizAnswerRecord userQuizAnswerRecord)
    {
        userQuizAnswerRecord.setUserId(SecurityUtils.getUserId());
        return userQuizAnswerRecordMapper.selectUserQuizAnswerRecordList(userQuizAnswerRecord);
    }

    /**
     * 新增用户答题记录管理
     * 
     * @param userQuizAnswerRecord 用户答题记录管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertUserQuizAnswerRecord(UserQuizAnswerRecord userQuizAnswerRecord)
    {
        // 1. 插入答题记录
        int result = userQuizAnswerRecordMapper.insertUserQuizAnswerRecord(userQuizAnswerRecord);

        if (result > 0) {
            // 积分记录和统计更新都应该在同一事务中
            pointsChangeRecordDao.recordAndUpdatePoints(
                    userQuizAnswerRecord.getUserId(),
                    userQuizAnswerRecord.getPointsEarned(),
                    1L,
                    CodeGeneratorUtil.generateBatchCode()
            );
            updateQuestionStats(userQuizAnswerRecord);
        }
        return result;
    }

    /**
     * 更新统计答题次数
     *
     * @param userQuizAnswerRecord
     */
    private void updateQuestionStats(UserQuizAnswerRecord userQuizAnswerRecord) {
        if (userQuizAnswerRecord.getIsCorrect()==1) {
            // 答对了，同时更新答题次数和正确次数
            questionInfoMapper.updateQuestionStatsWithCorrect(userQuizAnswerRecord.getQuestionId());
        } else {
            // 答错了，只更新答题次数
            questionInfoMapper.updateQuestionStatsWithWrong(userQuizAnswerRecord.getQuestionId());
        }
    }

    /**
     * 修改用户答题记录管理
     * 
     * @param userQuizAnswerRecord 用户答题记录管理
     * @return 结果
     */
    @Override
    public int updateUserQuizAnswerRecord(UserQuizAnswerRecord userQuizAnswerRecord)
    {
        return userQuizAnswerRecordMapper.updateUserQuizAnswerRecord(userQuizAnswerRecord);
    }

    /**
     * 批量删除用户答题记录管理
     * 
     * @param ids 需要删除的用户答题记录管理主键
     * @return 结果
     */
    @Override
    public int deleteUserQuizAnswerRecordByIds(Long[] ids)
    {
        return userQuizAnswerRecordMapper.deleteUserQuizAnswerRecordByIds(ids);
    }

    /**
     * 删除用户答题记录管理信息
     * 
     * @param id 用户答题记录管理主键
     * @return 结果
     */
    @Override
    public int deleteUserQuizAnswerRecordById(Long id)
    {
        return userQuizAnswerRecordMapper.deleteUserQuizAnswerRecordById(id);
    }
}
